<div class="form">
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
	'id'=>'supplier-evaluation-form',
	'enableAjaxValidation'=>false,
        'method'=>'post',
	'type'=>'horizontal',
	'htmlOptions'=>array(
		'enctype'=>'multipart/form-data'
	)
)); ?>
     	<fieldset>
		<legend>
			<p class="note">Fields with <span class="required">*</span> are required.</p>
		</legend>

	<?php echo $form->errorSummary($model,'Opps!!!', null,array('class'=>'alert alert-error span12')); ?>
        		
   <div class="control-group">		
	<span class="span7">
	<?php echo $form->textFieldRow($model,'evaluation_number',array('readonly'=>true,'class'=>'span4','maxlength'=>-1)); ?>
	
	<?php echo $form->dropDownListRow($model,'supplier_code',CHtml::listData(Suppliers::model()->findAll(), 'id', 'name'), array('prompt'=>'Choose Supplier', 'class'=>'span4','maxlength'=>-1)); ?>

	<?php //echo $form->textFieldRow($model,'evaluation_number_seq',array('class'=>'span4')); ?>

	<?php echo $form->textFieldRow($model,'revision_number',array('class'=>'span4','maxlength'=>-1)); ?>
	
	<?php echo $form->textFieldRow($model,'date_from',array('id'=>'datepicker', 'class'=>'form-control')); ?>

	<?php echo $form->textFieldRow($model,'date_thru',array('id'=>'datepicker2', 'class'=>'form-control')); ?>
	
	<?php echo $form->textAreaRow($model,'note',array('class'=>'span4','maxlength'=>-1)); ?>
	

	<?php echo $form->textField($model,'remarks',array('id'=>'rem','style'=>'display:none;','class'=>'span5','maxlength'=>-1)); ?>

	<?php echo $form->textField($model,'ranking',array('id'=>'ran','style'=>'display:none;','class'=>'span5','maxlength'=>-1)); ?>

	<?php echo $form->textField($model,'total_mark',array('id'=>'ave','style'=>'display:none;','class'=>'span5','maxlength'=>18)); ?>

	<?php echo $form->textField($model,'evaluation_date',array('value'=>date('Y-m-d H:i:s'),'style'=>'display:none;','class'=>'span5')); ?>
 </span>
 <span class="span5 last">
 <table class="items table table-striped table-bordered table-condensed">
 <thead>
 <tr>
	<th>NO</th>
	<th>Accesment Category</th>
	<th>Mark</th>
 </tr>
 </thead>
 <tbody>
 <?php $evals = EvaluationCategory::model()->findAll(); 
 $i=0;
 foreach($evals as $eval):
 ?>
  <tr>
	<td><?php echo $i+1; ?></td>
	<td><?php echo $eval->name; ?></td>
	<td><input class="sum" type="text" name="<?php echo $eval->id; ?>"/></td>
 </tr>
 
 <?php $i++; ?>
 <?php endforeach; ?>

 </tbody>
 </table>
 <div id="hasil">Average Mark : <span id="average"><?php echo $model->total_mark; ?></span> Ranking : <span id="rank"><?php echo $model->ranking; ?></span> Remark : <span id="remark"><?php echo $model->remarks; ?></span></div>
  <script type="text/javascript">
 $(document).ready(function(){
 
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".sum").each(function() {
 
            $(this).keyup(function(){
                calculateSum();
            });
        });
 
    });
 
    function calculateSum() {
 
        var sum = 0;
		var kal = 0;
        //iterate through each textboxes and add the values
        $(".sum").each(function() {
 
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
 
        });
		
		kal = sum/<?php echo count($evals); ?>;
        //.toFixed() method will roundoff the final sum to 2 decimal places
		//console.log(kal);
		if(kal >= 0 && kal <=3){
			$("#rank").html('E');
			$("#remark").html('Veri Bad');
			$("#ran").val('E');
			$("#rem").val('Veri Bad');
		}
		if(kal > 3 && kal <=5){
			$("#rank").html('D');
			$("#remark").html('Bad');
			$("#ran").val('D');
			$("#rem").val('Bad');
		}
		if(kal > 5 && kal <= 6){
			$("#rank").html('C');
			$("#remark").html('Enough');
			$("#ran").val('C');
			$("#rem").val('Enough');
		}
		if(kal > 6 && kal <= 8){
			$("#rank").html('B');
			$("#remark").html('Good');
			$("#ran").val('B');
			$("#rem").val('Good');
		}
		if(kal > 8){
			$("#rank").html('A');
			$("#remark").html('Excelent');
			$("#ran").val('A');
			$("#rem").val('Excelent');
		}
         $("#average").html(kal);
         $("#ave").val(kal);
    }
 </script>
  Note:<br />
Mark Point: Range 0 - 10
  <table width="100%">

<tr>
    <td>0 - 3 </td><td>:</td><td> E (Very Bad)</td>
</tr>
<tr>
   <td>3 - 5 </td><td>:</td><td> D (Bad)</td>
</tr>
<tr>
    <td>5 - 6 </td><td>:</td><td> C (Enough)</td>
</tr>
<tr>
    <td>6 - 8 </td><td>:</td><td> B (Good)</td>
</tr>
<tr>
    <td>> 8   </td><td>:</td><td> A (Excellent)</td>
</tr>
 </table>
 </span>
  </div>

	<div class="form-actions">
		<?php $this->widget('bootstrap.widgets.TbButton', array(
			'buttonType'=>'submit',
			'type'=>'primary',
                        'icon'=>'ok white',  
			'label'=>$model->isNewRecord ? 'Create' : 'Save',
		)); ?>
              <?php $this->widget('bootstrap.widgets.TbButton', array(
			'buttonType'=>'reset',
                        'icon'=>'remove',  
			'label'=>'Reset',
		)); ?>
	</div>
</fieldset>

<?php $this->endWidget(); ?>

</div>
